﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Abp.Application.Services.Dto;
using Abp.Domain.Repositories;
using Abp.Organizations;
using Abp.Web.Models;
using DualControl.Application.Organizations;
using DualControl.Application.Organizations.Dto;
using DualControl.Controllers;
using DualControl.Organizations;
using DualControl.Organizations.Dto;
using DualControl.Users;
using Microsoft.AspNetCore.Mvc;

using X.PagedList;

namespace DualControl.Web.Mvc.Controllers
{
    public class OrganizationsController : DualControlControllerBase
    {
        private readonly IOrganizationUnitAppService _organizationUnitAppService;

        private readonly IUserAppService _userAppService;

        public OrganizationsController(IOrganizationUnitAppService organizationUnitAppService,IUserAppService userAppService)
        {
             _organizationUnitAppService = organizationUnitAppService;
            _userAppService = userAppService;
        }

        public async Task<ActionResult> Index(int page)
        {
            int pagenumber = page < 1 ? 1 : page;
            GetOrganizationUnitInput input = new GetOrganizationUnitInput
            {
                SkipCount = (pagenumber - 1) * AppConsts.DefaultPageSize,
                MaxResultCount = AppConsts.DefaultPageSize
            };

            var orgs = (await _organizationUnitAppService.GetPagedOrganizationUnits(input));

            var PageOfOrgsListDto = new StaticPagedList<OrganizationUnitDto>(orgs.Items, pagenumber, AppConsts.DefaultPageSize, orgs.TotalCount);
            
            //将分页结果放入ViewBag供View使用
            ViewBag.PageOfOrgsListDto = PageOfOrgsListDto;

            return View();
        }
        [DontWrapResult]
        public JsonResult GetAllOrganizations()
        {

            var orgList = _organizationUnitAppService.GetOrganizationUnits();
            return Json(orgList);         
        }
        //未使用
        public ActionResult EditOrganizationModal (int orgid)
        {
            var model = new { };
            return View("_EditOrgModal", model);
        }

        //未使用
        //public PartialViewResult CreateModal(long? parentId)
        //{
        //    return PartialView("_CreateModal", new CreateOrganizationUnitModalViewModel(parentId));
        //}
    }
}